<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\LinkRequest;
use App\Models\LinkModel;
use App\Http\Requests\Admin\LinkEditRequest;

class LinkController extends Controller
{
    /**
     * Display a listing of the resource.
     * 加载友情链接列表
     *  
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //获取所有数据
        $count = $request -> input('count',5);
        $search = $request -> input('search','');
        $request = $request -> all();

        $data = LinkModel::where('lname','like','%'.$search.'%')->paginate($count);

        return view('admin.link.index',['title'=>'友情链接详情','data'=>$data,'request'=>$request,'count'=>$count,'search'=>$search]);
    }

    /**
     * 加载友情链接添加页面
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.link.create',['title'=>'添加友情链接']);
    }

    /**
     * 实行添加数据-逻辑业务
     * Store a newly created resource in storage.
     *
     * @param  LinkRequest  表单验证
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(LinkRequest $request)
    {
        //获取提交的数据
        $data = $request -> all();
        // 处理插入
        $link = new LinkModel;
        $link -> lname = $data['lname'];
        $link -> llink = $data['llink'];
        $link -> lstatus = $data['lstatus'];

        if($link->save()){
            return redirect('/admin/link')->with('success','添加成功');
        }else{
            return back()->withInput('error','添加失败');
        }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * 展示修改的页面
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return 修改页面
     */
    public function edit($id)
    {   
        $data = LinkModel::find($id);
        $title = '友情链接修改';

        return view('admin.link.edit',compact('data','title','id'));
    }

    /**
     * 实行修改的操作  
     * Update the specified resource in storage.
     *  
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return 返回视图
     */
    public function update(LinkEditRequest $request, $id)
    {
        //
        $data = $request->except('_token','_method');
        $link = LinkModel::find($id);
        $link -> lname = $data['lname'];
        $link -> llink = $data['llink'];

        if($link->save()){
            return redirect('/admin/link')->with('success','修改成功');
        }else{
            return back()->withInput('error','修改失败');
        }
    }

    /**
     * ajax实现的删除的功能
     *
     * @param  int  $id  删除传值的id
     * @return $data ajax返回的json数据
     */
    public function destroy($id)
    {
        //
        // console.log($id);
        $data = [];
        $re = LinkModel::find($id)->delete();
        if($re){
            $data['status'] = 0;
            $data['msg'] = '删除成功';
        }else{
            $data['status'] = 1;
            $data['msg'] = '删除成功';
        }
        return $data;
    }
}
